package com.lbtc.controller;

import com.lbtc.common.ApiRestResponse;
import com.lbtc.common.Constant;
import com.lbtc.domian.User;
import com.lbtc.exception.LbtcMallException;
import com.lbtc.exception.LbtcMallExceptionEnum;
import com.lbtc.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/test")
    @ResponseBody
    public User personalPage(Integer id) {
        User user = userService.getUserById(id);
        return user;
    }

    @PostMapping("/register")
    @ResponseBody
    public ApiRestResponse register(@PathParam("username") String username, @PathParam("password") String password) throws LbtcMallException {
        if (StringUtils.isEmpty(username)) {
            return ApiRestResponse.error(LbtcMallExceptionEnum.NEED_USER_NAME);
        }
        if (StringUtils.isEmpty(password)) {
            return ApiRestResponse.error(LbtcMallExceptionEnum.NEED_PASSWORD);
        }
        userService.registUser(username, password);
        return ApiRestResponse.success();
    }

    @ApiOperation("自写登录")
    @PostMapping("/login")
    @ResponseBody
    public ApiRestResponse login(@RequestBody User user, HttpSession session) throws LbtcMallException {
        if (StringUtils.isEmpty(user.getUsername())) {
            return ApiRestResponse.error(LbtcMallExceptionEnum.NEED_USER_NAME);
        }
        if (StringUtils.isEmpty(user.getPassword())) {
            return ApiRestResponse.error(LbtcMallExceptionEnum.NEED_PASSWORD);
        }
        if (user.getPassword().length() < 8) {
            return ApiRestResponse.error(LbtcMallExceptionEnum.PASSWORD_TOO_LONG_OR_SHORT);
        }
        User loginUser = userService.login(user);
        loginUser.setPassword("密码不可见");
        session.setAttribute(Constant.LBTC_MALL_USER, loginUser);
        //角色判断
        Integer role = loginUser.getRole();
        if (role == 2) {
            System.out.println("管理员登录成功");
            return ApiRestResponse.success(loginUser);
        }
        return ApiRestResponse.success(loginUser);
    }
    @ApiOperation("Lb登录")
    @GetMapping("/lbtclogin")
    @ResponseBody
    public ApiRestResponse lbtcLogin(@RequestParam("username") String username, @RequestParam("password") String password, HttpSession session) throws LbtcMallException {
        if (StringUtils.isEmpty(username)) {
            return ApiRestResponse.error(LbtcMallExceptionEnum.NEED_USER_NAME);
        }
        if (StringUtils.isEmpty(password)) {
            return ApiRestResponse.error(LbtcMallExceptionEnum.NEED_PASSWORD);
        }
        if (username.length() < 8) {
            return ApiRestResponse.error(LbtcMallExceptionEnum.PASSWORD_TOO_LONG_OR_SHORT);
        }
        User loginUser = userService.lbtcLogin(username, password);
        session.setAttribute(Constant.LBTC_MALL_USER, loginUser);//好像没执行
//        System.out.println("------"+session.getAttribute(Constant.LBTC_MALL_USER));
        loginUser.setPassword("密码不可见");
        return ApiRestResponse.success(loginUser);
    }

    @GetMapping("/update")
    @ResponseBody
    public ApiRestResponse modify(@RequestBody User user, HttpSession session) throws LbtcMallException {
        User sessionUser = (User) session.getAttribute(Constant.LBTC_MALL_USER);
        if (sessionUser == null) {
            return ApiRestResponse.error(LbtcMallExceptionEnum.NEED_LOGIN);
        }
        user.setId(sessionUser.getId());
        userService.updateInformation(user);
        return ApiRestResponse.success(user);
    }

    //@PathVariable("signature") => MissingPathVariableException: Missing URI template variable 'signature' for method parameter of type String
    //@RequestBody json=>{"personalizedSignature":"UpdateTest"}
    //@RequestParam 通过url给值正常
    //没传signature throws LbtcMallException
    @GetMapping("/lbtcupdate")
    @ResponseBody
    public ApiRestResponse lbtcUpdate(@RequestParam String signature, HttpSession session) throws LbtcMallException {
        User currentUser = (User) session.getAttribute(Constant.LBTC_MALL_USER);
        if (currentUser == null) {
            return ApiRestResponse.error(LbtcMallExceptionEnum.NEED_LOGIN);
        }
        User user = new User();
        user.setId(currentUser.getId());
        user.setPersonalizedSignature(signature);
        userService.updateInformation(user);
//        try {
//            userService.updateInformation(user);
//        } catch (LbtcMallException e) {
//            e.printStackTrace();
//            return ApiRestResponse.error(LbtcMallExceptionEnum.ERROR_UPDATE);
//        }
        return ApiRestResponse.success(user);
    }

    @PostMapping("/user/logout")
    @ResponseBody
    public ApiRestResponse logout(HttpSession session) {
        session.removeAttribute(Constant.LBTC_MALL_USER);
        return ApiRestResponse.success();
    }
}
